﻿using MediatR;
using SystemMHB.API.Application.Command.LogisticsCommand;
using SystemMHB.Infrstructer.Interface;

namespace SystemMHB.API.Application.CommandHandlers.LogisticsCommandHandler
{
    /// <summary>
    /// 物资信息中介者
    /// </summary>
    public class LogisticsAddCommandHandler:IRequestHandler<LogisticsAddCommand,int>
    {
        /// <summary>
        /// 物资信息仓储接口
        /// </summary>
        ILogisticsReposiyory _logistics;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_logistics">物资信息仓储接口</param>
        public LogisticsAddCommandHandler(ILogisticsReposiyory _logistics)
        {
            this._logistics = _logistics;
        }

        public async Task<int> Handle(LogisticsAddCommand request, CancellationToken cancellationToken)
        {
            var nian = DateTime.Now.Year.ToString();
            var yue = DateTime.Now.Month.ToString();
            var ri = DateTime.Now.Day.ToString();
            var si = DateTime.Now.Hour.ToString();
            var fn = DateTime.Now.Minute.ToString();
            var miao = DateTime.Now.Second.ToString();

            var orderNumber = "DD"+ nian+ yue+ ri+ si+ fn+ miao;
            var creationTime = DateTime.Now.ToUniversalTime();//创建时间
            var paymentTime = DateTime.Now.ToUniversalTime();//支付时间
            return await _logistics.AddAsync(new Domian.Logistics()
            {
                orderID = request.orderID,
                purchaseNumber = request.purchaseNumber,
                orderNumber = orderNumber,
                creationTime = creationTime,
                paymentTime = paymentTime,
                logisticsType = 1
            });
        }
    }
}
